package com.sskj.boom.ui.fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.boom.R2;
import com.sskj.boom.bean.StockBloomBean;
import com.sskj.boom.component.DaggerUserDataComponent;
import com.sskj.boom.presenter.AllGuessPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;

import com.sskj.boom.R;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.util.HideUtil;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.widget.LinearLayout;

import org.reactivestreams.Subscription;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.FlowableSubscriber;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

/**
 * @author Hey
 * Create at  2019/06/27
 */
@Route(path = RConfig.BOOM_FRAGMENT_ALL_GUESS)
public class AllGuessFragment extends BaseFragment<AllGuessPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private String userid = "";
    private SlimAdapter slimAdapter;
    private List<StockBloomBean.DataBean.XiazhuBeans> xiazhu_list = new ArrayList<>();
    private BaseAdapter<String> adapter;
    private Disposable subscribe;


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeFive(StockBloomBean stockBloomBean) {
        if (stockBloomBean != null) {
            if (slimAdapter == null) {
                return;
            }
            if (stockBloomBean.getData().get(1).getXiazhu_list() == null
                    || stockBloomBean.getData().get(1).getXiazhu_list().size() == 0) {
                slimAdapter.updateData(new ArrayList());
                return;
            }
            subscribe = Flowable.fromIterable(stockBloomBean.getData().get(1).getXiazhu_list())
                    .filter(xiazhuBeans -> xiazhuBeans.getUid().equals(userid))
                    .compose(RxSchedulersHelper.transformer())
                    .subscribe(xiazhuBeans -> {
                                xiazhu_list.clear();
                                xiazhu_list.add(xiazhuBeans);
                            }
                            , e -> e.printStackTrace()
                            , () -> slimAdapter.updateData(xiazhu_list));
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.boom_fragment_all_guess;
    }

    @Override
    public AllGuessPresenter getPresenter() {
        return new AllGuessPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        RxBus.getDefault().register(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        slimAdapter = SlimAdapter.create().register(R.layout.boom_item_all_guess, new SlimInjector<StockBloomBean.DataBean.XiazhuBeans>() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onInject(StockBloomBean.DataBean.XiazhuBeans data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.background(R.id.llBg, ContextCompat.getDrawable(getActivity(), R.drawable.lib_solidfff_cor10));
                } else {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(getActivity(), R.color.transparent));
                }
                String palyerName = data.getRealname();
                if (HideUtil.isInteger(palyerName)) {
                    if (palyerName.length() == 11) {
                        palyerName = HideUtil.getPhoneHide(palyerName);
                    }
                }
                if (TextUtils.isEmpty(data.getUpic())) {
                    injector.image(R.id.iv_header, App.INSTANCE.getResources().getDrawable(R.mipmap.icon_head));
                } else {
                    injector.image(R.id.iv_header, BaseHttpConfig.BASE_URL + data.getUpic());
                }
                injector.text(R.id.tv_player, palyerName)
                        .text(R.id.tv_put, NumberUtil.keepNoZero(data.getBuy_money()) + data.getPid_name());
                int status = data.getStatus();
                switch (status) {
                    case 1:
                        injector.text(R.id.tv_run, "--")
                                .text(R.id.tv_income, "--")
                                .textColor(R.id.tv_run, ContextCompat.getColor(getActivity(), R.color.commonText333))
                                .textColor(R.id.tv_income, ContextCompat.getColor(getActivity(), R.color.commonText333));
                        break;
                    case 2:
                        BigDecimal bet = new BigDecimal(data.getFinal_beishu());
                        BigDecimal money = new BigDecimal(data.getBuy_money());
                        injector.text(R.id.tv_run, data.getFinal_beishu() + "X")
                                .text(R.id.tv_income, NumberUtil.keep4(bet.multiply(money).toString()) + data.getPid_name())
                                .textColor(R.id.tv_run, ContextCompat.getColor(getActivity(), R.color.libUpColor))
                                .textColor(R.id.tv_income, ContextCompat.getColor(getActivity(), R.color.libDownColor));
                        break;
                    case 3:
                        injector.text(R.id.tv_run, data.getEscape_beishu() + "X")
                                .text(R.id.tv_income, "-" + data.getBuy_money() + data.getPid_name())
                                .textColor(R.id.tv_run, ContextCompat.getColor(getActivity(), R.color.commonText333))
                                .textColor(R.id.tv_income, ContextCompat.getColor(getActivity(), R.color.commonText333));
                        break;
                }
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
    }

    @Override
    public void initData() {

        /*获取用户信息*/
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                userid = userData.getUid();
            } else {
                userData = null;
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DisposUtil.close(subscribe);
    }

    public static AllGuessFragment newInstance() {
        AllGuessFragment fragment = new AllGuessFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


}
